package com.hkemmm.alipay.controller;

import com.alipay.api.AlipayApiException;
import com.hkemmm.alipay.bean.AliPayBean;
import com.hkemmm.alipay.config.Alipay;
import com.hkemmm.alipay.service.IPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/ali")
public class OrderController1 {

    /**
     * 日志对象
     */
    private static final Logger logger = LoggerFactory.getLogger(OrderController1.class);

    @Autowired
    private IPayService payService;

    @RequestMapping("/pay")
    public void alipay(String outTradeNo, String subject, String totalAmount, HttpServletResponse response) throws AlipayApiException, IOException {
        System.out.println(outTradeNo);
        System.out.println(subject);
        System.out.println(totalAmount);

        logger.info("商户订单号为{},订单名称为{},付款金额为{}", outTradeNo, subject, totalAmount);
        AliPayBean alipayBean = new AliPayBean();
        alipayBean.setOut_trade_no(outTradeNo);
        alipayBean.setSubject(subject);
        alipayBean.setTotal_amount(totalAmount);


        response.setContentType("text/html;charset=utf-8");
        String s = payService.aliPay(alipayBean);
        response.getWriter().write(s);//直接将完整的表单html输出到页面
        response.getWriter().flush();
        response.getWriter().close();
    }

    @RequestMapping("/success")
    @ResponseBody
    public String success() {
        return "交易成功！";
    }

    @RequestMapping(value = "/index")
    public String payCoin() {
        return "zfindex.html";
    }
}
